﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Skywave.Reporting
{
    public class HtmlReportFieldDateTime : HtmlReportField
    {
        public HtmlReportFieldDateTime()
        {
            _Calendar = DateTimeHelperCalendars.Persian;
            _Format = DateTimeHelperDisplayFormats.ShortDate;
            _Kind = DateTimeKind.Local;
            _IncludeTime = false;
        }

        public DateTimeHelperCalendars _Calendar { get; set; }
        public DateTimeHelperDisplayFormats _Format { get; set; }
        public DateTimeKind? _Kind { get; set; }
        public bool _IncludeTime { get; set; }
        public string _TimeFormat { get; set; }

        internal override string GetContent(HtmlReportFieldGetContentArgs e)
        {
            DateTime? eValue = e.Value as DateTime?;
            if (string.IsNullOrEmpty(_TimeFormat))
                return new DateTimeHelper(_Calendar, (eValue == null) ? DateTime.UtcNow : eValue, _Kind).ToFormatedString(_Format, _IncludeTime);
            else
                return new DateTimeHelper(_Calendar, (eValue == null) ? DateTime.UtcNow : eValue, _Kind).ToFormatedString(_Format, _TimeFormat);
        }

        internal override void Create_Internal(string toParse)
        {
            using (System.IO.StringReader sr1 = new System.IO.StringReader(HtmlReport.Parsing_FixToParse(toParse)))
            {
                System.Xml.XmlReader xmlR1 = HtmlReport.Parsing_GetXmlReader(sr1);
                //
                while (xmlR1.Read())
                {
                    if (xmlR1.Depth == 0 && xmlR1.IsStartElement("ReportField", "http://skywavetech.ir/skywave/reporting"))
                    {
                        string s1;
                        //
                        s1 = xmlR1.GetAttribute("Calendar");
                        if (!string.IsNullOrEmpty(s1))
                            _Calendar = (DateTimeHelperCalendars)Enum.Parse(typeof(DateTimeHelperCalendars), s1, true);
                        //
                        s1 = xmlR1.GetAttribute("Format");
                        if (!string.IsNullOrEmpty(s1))
                            _Format = (DateTimeHelperDisplayFormats)Enum.Parse(typeof(DateTimeHelperDisplayFormats), s1, true);
                        //
                        s1 = xmlR1.GetAttribute("Kind");
                        if (!string.IsNullOrEmpty(s1))
                            _Kind = (DateTimeKind)Enum.Parse(typeof(DateTimeKind), s1, true);
                        //
                        s1 = xmlR1.GetAttribute("IncludeTime");
                        if (!string.IsNullOrEmpty(s1))
                            _IncludeTime = bool.Parse(s1);
                        //
                        s1 = xmlR1.GetAttribute("TimeFormat");
                        if (!string.IsNullOrEmpty(s1))
                            _TimeFormat = s1;
                    }
                }
            }
        }
    }
}
